﻿@using Dp.Wasm.Hubs
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.SignalR.Client
<CascadingAuthenticationState>
    <CascadingValue Value="this">
        <BootstrapBlazorRoot>
            <Router AppAssembly="@typeof(App).Assembly" AdditionalAssemblies="new System.Reflection.Assembly[]{typeof(SharedPage.JsInterOp).Assembly}">
                <Found Context="routeData">
                    <RouteView RouteData="@routeData" DefaultLayout="@typeof(BaseLayout)">
                        @* <NotAuthorized>
                            <span>没有权限,即将跳转..</span>
                        </NotAuthorized> *@
                    </RouteView>
                    <FocusOnNavigate RouteData="@routeData" Selector="h1" />
                </Found>
                <NotFound>
                    <PageTitle>Not found</PageTitle>
                    <LayoutView Layout="@typeof(BaseLayout)">
                        <p role="alert">Sorry, there's nothing at this address.</p>
                    </LayoutView>
                </NotFound>
            </Router>
        </BootstrapBlazorRoot>
    </CascadingValue>
</CascadingAuthenticationState>
@code {
    private HubConnection? hubConnection;
    [Inject]
    [NotNull]
    public NavigationManager? Navigation { get; set; }
    [Inject]
    [NotNull]
    public JsInterOp? JsEcharts { get; set; }
    [Inject]
    [NotNull]
    private IThemeProvider? ThemeProvider { get; set; }
    [Inject]
    public IJSRuntime? jSRuntime  { get; set; }
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        var theme =await jSRuntime.InvokeAsync<string>("localStorage.getItem","theme");
        await ThemeProvider.SetThemeAsync(theme);
      
        if (firstRender)
        {
            //获取数据,后期应该是一个接口,注入不同的后去方式,目前来自于本地db
            return;
            try
            {
                hubConnection = new HubConnectionBuilder()
                 .WithUrl(Navigation.ToAbsoluteUri("/chathub"))
                 .WithAutomaticReconnect(new SignalRReconnectInterVal())
                .Build();
                hubConnection.On<string, object>("ReceiveOption", (id, option) =>
                {
                    this.InvokeAsync(StateHasChanged);
                });
                await hubConnection.StartAsync();
                var sss = hubConnection.State == HubConnectionState.Connected ? "连接成功" : "连接失败";
                await JsEcharts.Log(sss);
            }
            catch (Exception ex)
            {
                await JsEcharts.Log("singlr连接失败:" + ex.Message);
            }
        }
    }
}
